/*
 * Player interface for vdjo.com
 * @classDescription player
 * @constructor
 * @return {Bool}
 */
function playerInterface(key) {
	
	this.key = key;
	this.state = null;
	this.id = null;
	this.api = null;
	this.cueing = false;
	this.ready = false;
	this.video = null;
	this.lastPlayStatVideoId = false;
	this.displayRemainingTime = true;
	
	this.init = function() {
		debug.print("playerInterface.init [key: " + this.key + "]");
		// set volume
		this.setVolume(100);
		this.getApi().unMute();
		this.setState(-1);
	}
		
	/**
	 * @method
	 * @param {String} id
	 */
	this.setId = function(id) {
		debug.print("playerInterface.setId [key: " + this.key + ", id: " + id + "]");
		this.id = id;
	}
	
	/**
	 * @method
	 */
	this.getId = function() {
		debug.print("playerInterface.getId [key: " + this.key + "]");
		return this.id;
	}
	
	/**
	 * Set the youtube javascript player api
	 * @method
	 * @param {Object} youtube javascript player api
	 */
	this.setApi = function(object) {
		debug.print("playerInterface.setApi [key: " + this.key + ", api: " + object + "]");
		this.api = object;
		this.api.addEventListener("onStateChange", "player["+this.key+"].setState");
	}
	
	/**
	 * Get the youtube javascript player api
	 * @method
	 * @return {Object} api
	 */
	this.getApi = function() {
		debug.print("playerInterface.getApi [key: " + this.key + "]");
		return this.api;
	}

	/**
	 * Video state changed
	 * @param {Integer} state
	 */
	this.setState = function(state) {
		debug.print("playerInterface.setState [key: " + this.key + ", state: " + state + " (" + playerState(state) + ")]");
		this.state = state;
		this.stateResponse();
	}
	
	this.getState = function() {
		debug.print("playerInterface.getState [key: " + this.key + "]");
		return this.state;
	}
		
	this.stateResponse = function() {
		debug.print("playerInterface.stateResponse [key: " + this.key + "]");
		if (this.getState() == 0) { // ended
			this.ready = false;
			if (autoplay.enabled === true && autoplay.fading === true) {
				autoplay.fadeStop();
				autoplay.loadNext(this.key);
			}
		} else if (this.getState() == 1) { // playing
			ui.setVideoStatus(this.key, "playing");
		} else if (this.getState() == 3) { // buffering 
			if (this.cueing == true) {
				// allow some time for buffering
				var key = this.key;
				setTimeout(function() {
					ui.setVideoStatus(key, "ready");
				}, 1000);
				this.pause();
				controller.faderChange();
				player[key].updateTime();
				this.cueing = false;
				this.ready = true;
				debug.print("Player " + this.key + " is ready to play", true);
			} else {
				ui.setVideoStatus(this.key, "loading");
			}
		}
	}
	
	this.cue = function(item) { 
		debug.print("playerInterface.cue [key: " + this.key + ", item: " + item + "]");
		this.video = item;
		
		ui.setVideoStatus(this.key, "loading");
		this.cueing = true;
		this.ready = false;
		this.prepareUi();
		
		this.setVolume(0);
		this.getApi().loadVideoById(this.getId(), 0, config.vidQuality);
	}
	
	this.loadAndPlay = function(item) {
		debug.print("playerInterface.loadAndPlay [key: " + this.key + ", item: " + item + "]");
		this.video = item;
		
		ui.updatePlayPause(this.key, 'pause');
		this.ready = true;
		this.prepareUi();
		
		this.getApi().loadVideoById(this.getId(), 0, config.vidQuality);
		this.recordPlayStat();
	}
	
	this.prepareUi = function() {
		debug.print("playerInterface.perpareUi [key: " + this.key + "]");
		this.setId(this.video.id);
		ui.setVideoTitle(this.key, this.video);
		
		if (this.video.download == 1) {
			download.showButton(key);
		} else {
			download.hideButton(key);	
		}
		
		if (this.video.fav == 1) {
			fav.on(key);
		} else {
			fav.off(key);
		}	
	}
	
	this.play = function() {
		debug.print("playerInterface.play [key: " + this.key + "]");
		this.getApi().playVideo();
		ui.updatePlayPause(this.key, 'pause');
		this.recordPlayStat();
	}
	
	this.recordPlayStat = function() {
		var key = this.key;
		hash.setHistory(this.video);
		ui.setWindowTitle(this.video.artist + ' - ' + this.video.title);
		if (this.video.id != this.lastPlayStatVideoId) {
			debug.print("playerInterface.recordPlayStat [key: " + this.key + "]");
			$.getJSON("/video/recordPlayStat/json?video_id=" + this.video.id, function(data){
				if (data.status == 1) {
					// success	
				}				
			});
			this.lastPlayStatVideoId = this.video.id;
		}
	}
	
	this.pause = function() {
		debug.print("playerInterface.pause [key: " + this.key + "]");
		this.getApi().pauseVideo();
		ui.updatePlayPause(this.key, 'play');
	}
	
	this.updateBufferStatus = function() {
		debug.print("playerInterface.updateBufferStatus [key: " + this.key + "]");
		if (this.state != -1) { // only if not unstarted
			var loaded = parseInt((this.getApi().getVideoBytesLoaded() / this.getApi().getVideoBytesTotal()) * 100);
			var started = parseInt((this.getApi().getVideoStartBytes() / this.getApi().getVideoBytesTotal()) * 100);
			ui.updateBuffer(this.key, loaded, started);
		}
	}
	
	this.updatePositionStatus = function() {
		debug.print("playerInterface.updatePositionStatus [key: " + this.key + "]");
		if (this.state == 1) { // only if playing
			var position = parseInt((this.getApi().getCurrentTime() / this.getApi().getDuration()) * 100);
			ui.updatePosition(this.key, position);
		}
	}
	
	this.updateTime = function() {
		debug.print("playerInterface.updateTime [key: " + this.key + "]");
		if (this.state == 1) { // only if playing
			if (this.displayRemainingTime) {
				var seconds = parseInt(this.getDuration() - this.getCurrentTime());
				var minutes = Math.floor(seconds / 60);
				var seconds = '' + (seconds % 60);
				while (seconds.length < 2) {
					seconds = '0' + seconds;
				}
				ui.updateTime(this.key, "-" + minutes + ":" + seconds);
			} else {
				var seconds = parseInt(this.getCurrentTime());
				var minutes = Math.floor(seconds / 60);
				var seconds = '' + (seconds % 60);
				while (seconds.length < 2) {
					seconds = '0' + seconds;
				}
				ui.updateTime(this.key, minutes + ":" + seconds);
			}
		}
	}
	
	this.setVolume = function(volume) {
		debug.print("playerInterface.setVolume [key: " + this.key + ", volume: " + volume + "]");
		this.getApi().setVolume(volume);
	}
	
	this.setPosition = function(perc) {
		debug.print("playerInterface.setVolume [key: " + this.key + ", perc: " + perc + "]");
		var sec = parseInt(this.getApi().getDuration() / 100 * perc);
		this.getApi().seekTo(sec, true);
	}
	
	this.getCurrentTime = function() {
		debug.print("playerInterface.getCurrentTime [key: " + this.key + "]");
		return this.getApi().getCurrentTime();
	}
	
	this.getDuration = function() {
		debug.print("playerInterface.getDuration [key: " + this.key + "]");
		return this.getApi().getDuration();
	}
	
	this.togglePlay = function() {
		debug.print("playerInterface.togglePlay [key: " + this.key + "]");
		if (this.ready) {
			if (this.isPlaying()) {
				this.pause();
			} else if (this.isPaused()) {
				this.play();
			} else {
				flash.flash('Video', 'ended or buffering');
				debug.print("Player " + this.key + " not playing and not paused...", true);
			}
		} else {
			flash.flash('No video', 'loaded');
			debug.print("Player " + this.key + " not ready...", true);
		}
	}
	
	this.isPlaying = function() {
		debug.print("playerInterface.isPlaying [key: " + this.key + "]");
		return (this.getState() == 1);
	}
	
	this.isPaused = function() {
		debug.print("playerInterface.isPaused [key: " + this.key + "]");
		return (this.getState() == 2);
	}
	
	return true;
};

function playerState(state) {
	switch (state) {
		case -1:
			return "unstarted";
			break;
		case 0:
			return "ended";
			break;
		case 1:
			return "playing";
			break;
		case 2:
			return "paused";
			break;
		case 3:
			return "buffering";
			break;
		case 5:
			return "video cued";
			break;	 
		default:
			return false;
	}
}